package com.tengju.data.infrastructure.repository.wx;

import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.model.wx.WxUserInfo;
import com.tengju.data.domain.model.wx.WxUserInfoRepository;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.WxUserInfoDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.tengju.custom.WxUserInfoDOCustomMapper;
import com.tengju.data.infrastructure.repository.convert.WxUserInfoConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 李大仙
 * @date: 2021/11/5 11:11 上午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class WxUserInfoRepositoryImpl implements WxUserInfoRepository {

    private final WxUserInfoDOCustomMapper wxUserInfoDOCustomMapper;

    @Override
    public int batchInsert(List<WxUserInfo> wxUserInfoList) {
        List<WxUserInfoDO> wxUserInfoDOList = wxUserInfoList.stream()
                .map(WxUserInfoConvert::convert)
                .collect(Collectors.toList());
        return wxUserInfoDOCustomMapper.batchInsert(wxUserInfoDOList);
    }

    @Override
    public int insertOrUpdate(WxUserInfo wxUserInfo) {
        WxUserInfoDO wxUserInfoDO = WxUserInfoConvert.convert(wxUserInfo);
        return wxUserInfoDOCustomMapper.insertOrUpdate(wxUserInfoDO);
    }

    @Override
    public void deleteByIdCode(UserIdCode idCode) {
        wxUserInfoDOCustomMapper.deleteByIdCode(idCode.getValue());
    }

    @Override
    public WxUserInfo selectByIdCode(UserIdCode idCode) {
        if (idCode == null) {
            return null;
        }
        WxUserInfoDO wxUserInfoDO = wxUserInfoDOCustomMapper.selectByIdCode(idCode.getValue());
        return WxUserInfoConvert.convert(wxUserInfoDO);
    }

    @Override
    public List<WxUserInfo> selectAll() {
        List<WxUserInfoDO> wxUserInfoDOList = wxUserInfoDOCustomMapper.selectAll();
        return wxUserInfoDOList.stream()
                .map(WxUserInfoConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public List<WxUserInfo> selectAllServiceStar() {
        List<WxUserInfoDO> wxUserInfoDOList = wxUserInfoDOCustomMapper.selectAllServiceStar();
        return wxUserInfoDOList.stream()
                .map(WxUserInfoConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public int updateTags(WxUserInfo wxUserInfo) {
        WxUserInfoDO wxUserInfoDO = WxUserInfoConvert.convert(wxUserInfo);
        return wxUserInfoDOCustomMapper.updateTags(wxUserInfoDO);
    }

    @Override
    public int updateHistoryDominationIdCode(WxUserInfo wxUserInfo) {
        WxUserInfoDO wxUserInfoDO = WxUserInfoConvert.convert(wxUserInfo);
        return wxUserInfoDOCustomMapper.updateHistoryDominationIdCode(wxUserInfoDO);
    }

    @Override
    public int updateActivateStatus(WxUserInfo wxUserInfo) {
        WxUserInfoDO wxUserInfoDO = WxUserInfoConvert.convert(wxUserInfo);
        return wxUserInfoDOCustomMapper.updateActivateStatus(wxUserInfoDO);
    }
}
